var express = require('express');
var router = express.Router();
const mongodb= require('../components/mongo');

/* GET home page. */
router.get('/',async function(req, res, next) {
   console.log(req.query);
   var m_uid = req.query.u_id;
   var m_aid = req.query.a_id;
   if(m_aid == undefined){
	   //don't' have id
	   res.json({	   
		       errno: 0,
		       errmsg: "",
		       data: {
		           res: false
		       }
	   })
   }else{
	   //one article containers many supports
	   const result = await mongodb.find("Article",{a_id:m_aid});
	   console.log(result)
	   var supports = [];
		if(result.length > 0){
			var article = result[0];
			var IhaveSupported = false;
			var count = 0;
			for(index in article.supports){
				if(article.supports[index].u_id == m_uid){
					IhaveSupported = true;
				}
				count ++;
			}
				
			if(IhaveSupported){
				//have supported
				res.json({
						   
						       errno: 0,
						       errmsg: "",
						       data: {
									num_support : count,
									is_supported : true,
						       }
						   
				})
				
			}else{
				//not supported
				res.json({
						   
						       errno: 0,
						       errmsg: "",
						       data: {
									num_support : count,
									is_supported : false,
						       }
						   
				})
				
			}

		}else{
			res.json({
					   
					       errno: 0,
					       errmsg: "",
					       data: {
					           res: false
					       }
					   
			})	
		}
	   
   }

});


// ### 获取帖子详细信息的点赞（GET）
// 访问的URL：host/get_support  
// 客户端需要指明是哪个帖子的详细信息（帖子id）：
// ```
// {
//     "a_id" : 13340
//		"u_id" : 138293
// }
// ```
// 服务端需要返回这个帖子下的获赞个数：
// ```
// {
//     "errno": 0,
//     "errmsg": "",
//     "data": {
//         "num_support" : 5
//			"is_supported" : true
//     }
// }
// ```


module.exports = router;